import { createRouter, createWebHistory } from 'vue-router'
import { authUtils } from '../utils/auth'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { title: '首页', showInNav: true, requiresAuth: false }
  },

  {
    path: '/city',
    name: 'City',
    component: () => import('../views/city/CityList.vue'),
    meta: { title: '城市特色', showInNav: true, requiresAuth: true }
  },
  {
    path: '/city/:id',
    name: 'CityDetail',
    component: () => import('../views/city/CityDetail.vue'),
    meta: { title: '城市详情', showInNav: false, requiresAuth: true }
  },
  {
    path: '/my-wishlist',
    name: 'MyWishlist',
    component: () => import('../views/city/MyWishlist.vue'),
    meta: { title: '我的心愿清单', showInNav: false, requiresAuth: true }
  },

  {
    path: '/travel-plan',
    name: 'TravelPlan',
    component: () => import('../views/travel-plan/TravelPlanList.vue'),
    meta: { title: '旅游计划', showInNav: true, requiresAuth: true }
  },
  {
    path: '/travel-plan/new',
    name: 'CreateTravelPlan',
    component: () => import('../views/travel-plan/CreateTravelPlan.vue'),
    meta: { title: '创建旅游计划', showInNav: false, requiresAuth: true }
  },
  {
    path: '/travel-plan/:id',
    name: 'TravelPlanDetail',
    component: () => import('../views/travel-plan/TravelPlanDetail.vue'),
    meta: { title: '旅游计划详情', showInNav: false, requiresAuth: true }
  },
  {
    path: '/travel-plan/:id/edit',
    name: 'EditTravelPlan',
    component: () => import('../views/travel-plan/EditTravelPlan.vue'),
    meta: { title: '编辑旅游计划', showInNav: false, requiresAuth: true }
  },
  {
    path: '/routes',
    name: 'RouteList',
    component: () => import('../views/route/RouteList.vue'),
    meta: { title: '我的路线', showInNav: true, requiresAuth: true }
  },
  {
    path: '/route-planning',
    name: 'RoutePlanning',
    component: () => import('../views/travel-plan/RoutePlanning.vue'),
    meta: { title: '路线规划', showInNav: false, requiresAuth: true }
  },

  {
    path: '/travel-note',
    name: 'TravelNote',
    component: () => import('../views/travel-note/TravelNoteList.vue'),
    meta: { title: '游记', showInNav: true, requiresAuth: true }
  },
  {
    path: '/travel-note/new',
    name: 'CreateTravelNote',
    component: () => import('../views/travel-note/CreateTravelNote.vue'),
    meta: { title: '编写游记', showInNav: false, requiresAuth: true }
  },
  {
    path: '/travel-note/:id/edit',
    name: 'EditTravelNote',
    component: () => import('../views/travel-note/EditTravelNote.vue'),
    meta: { title: '修改游记', showInNav: false, requiresAuth: true }
  },
  {
    path: '/travel-note/:id',
    name: 'TravelNoteDetail',
    component: () => import('../views/travel-note/TravelNoteDetail.vue'),
    meta: { title: '游记详情', showInNav: false, requiresAuth: true }
  },

  {
    path: '/community',
    name: 'Community',
    component: () => import('../views/community/CommunityList.vue'),
    meta: { title: '旅游圈', showInNav: true, requiresAuth: true }
  },
  {
    path: '/community/:id',
    name: 'CommunityDetail',
    component: () => import('../views/community/CommunityDetail.vue'),
    meta: { title: '帖子详情', showInNav: false, requiresAuth: true }
  },

  {
    path: '/ai-assistant',
    name: 'AIAssistant',
    component: () => import('../views/ai/AiAssistant.vue'),
    meta: { title: 'AI助手', showInNav: true, requiresAuth: true }
  },

  {
    path: '/budget',
    name: 'Budget',
    component: () => import('../views/budget/Budget.vue'),
    meta: { title: '记账预算', showInNav: true, requiresAuth: true }
  },
  {
    path: '/budget/new',
    name: 'CreateBudget',
    component: () => import('../views/budget/CreateBudget.vue'),
    meta: { title: '新增记账', showInNav: false, requiresAuth: true }
  },
  {
    path: '/budget/:id',
    name: 'BudgetDetail',
    component: () => import('../views/budget/BudgetDetail.vue'),
    meta: { title: '记账详情', showInNav: false, requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/user/Profile.vue'),
    meta: { title: '个人中心', showInNav: false, requiresAuth: true }
  },
  {
    path: '/profile/favorites',
    name: 'Favorites',
    component: () => import('../views/user/Favorites.vue'),
    meta: { title: '我的收藏', showInNav: false, requiresAuth: true }
  },
  {
    path: '/profile/likes',
    name: 'Likes',
    component: () => import('../views/user/Likes.vue'),
    meta: { title: '我的喜欢', showInNav: false, requiresAuth: true }
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - Trave旅行日记` : 'Trave旅行日记'

  // 检查是否需要登录权限
  const requiresAuth = to.meta.requiresAuth !== false
  const isAuthenticated = authUtils.isLoggedIn()

  if (requiresAuth && !isAuthenticated) {
    // 未登录用户访问需要权限的页面
    if (typeof window !== 'undefined') {
      // 触发登录弹窗事件
      window.dispatchEvent(new CustomEvent('show-login-modal', {
        detail: {
          message: '请先登录后再访问',
          redirectPath: to.fullPath
        }
      }))
    }

    // 阻止导航，停留在当前页面
    next(false)

    // 如果当前不在首页，则跳转到首页
    if (from.path !== '/') {
      next('/')
    }
  } else {
    next() // 继续导航
  }
})

export default router